home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Cream of the Crop 21
/
Cream of the Crop 21 (Terry Blount) (October 1996).iso
/
program
/
srcbkvt.zip
/
ASC.ZIP
/
AWKPB.ASC
< prev
next >
Wrap
Text File
|
1996-07-08
|
4KB
|
163 lines
_Adding Power to PowerBuilder_
by Baylor Wetzel
Listing One
Protected:
int ii_file_handle // File Handle
int ii_num_fields // Equiv of $nf
long il_num_lines // Equiv of $nr
string is_current_line // Equiv of $0
string isa_fields[] // Fields
Public:
string is_file_name
int ii_error_status
/* Error Return Code Constants - 5.0 only */
constant int NO_ERROR = 0
constant int NO_FILE = -1
constant int FILE_NOT_EXIST = -2
constant int CANNOT_OPEN_FILE = -3
constant int FILE_READ_ERROR = -4
constant int INVALID_FIELD = -5
Listing Two
public string field( int ai_field )
public int nf( )
public long nr( )
protected parse_file( )
public int parse_line( string as_line )
public start( )
Listing Three
/* Make sure they set the file name */
IF ( is_file_name = "" ) THEN
ii_error_status = NO_FILE
TriggerEvent( "ue_error" )
return
END IF
/* Open the file, read only in line mode */
ii_file_handle = FileOpen(is_file_name, LineMode!, Read!)
IF ( ii_file_handle = -1 ) THEN
ii_error_status = CANNOT_OPEN_FILE
TriggerEvent( "ue_error" )
return
END IF
parse_file( )
Listing Four
/* FileRead return types */
int END_OF_FILE = -100
int READ_ERROR = -1
int NO_CHAR = 0
int li_num_char_read
/* We haven't read anything yet, so set the number of lines to 0 */
il_num_lines = 0
/* Start in the Begin event */
TriggerEvent( "ue_begin" )
/* Read through this until we hit the end of the file */
DO
li_num_char_read = FileRead( ii_file_handle, is_current_line )
CHOOSE CASE li_num_char_read
CASE READ_ERROR
ii_error_status = FILE_READ_ERROR
TriggerEvent( "ue_error" )
CASE END_OF_FILE
// Nothing, the loop will terminate
CASE ELSE
parse_line( is_current_line )
il_num_lines = il_num_lines + 1
TriggerEvent( "ue_scan" )
END CHOOSE
LOOP WHILE ( li_num_char_read <> END_OF_FILE )
FileClose( ii_file_handle )
TriggerEvent( "ue_end" )
Listing Five
int NO_MORE_DELIMITERS = 0 // We've hit the end of the line
long ll_pos // position of the delimiter within the string
/* We are just starting, so set the number of fields to 0 */
ii_num_fields = 0
/* Get the string ready */
as_line = Trim( as_line )
/* Figure out where the first delimiter is */
ll_pos = Pos( as_line, " " )
/* Parse until position is 0 (which means 1 more record exists) */
DO WHILE ( ll_pos <> NO_MORE_DELIMITERS )
/* We've found one, so increase our field counter */
ii_num_fields = ii_num_fields + 1
/* Store the field */
isa_fields[ii_num_fields] = Trim( Left( as_line, ll_pos))
/* Remove that part from our string */
as_line = Trim( mid( as_line, ll_pos ))
/* And find the next delimiter - if 0, allow one more go-round */
ll_pos = Pos( as_line, " ")
LOOP
/* One more record may be left, and if so, store it */
IF ( as_line <> "" ) THEN
ii_num_fields = ii_num_fields + 1
isa_fields[ii_num_fields] = Trim( as_line )
END IF
return ii_num_fields
Listing Six
public string field( int ai_field ):
CHOOSE CASE ai_field
CASE 0
return is_current_line
CASE 1 to nf( )
return isa_fields [ ai_field ]
CASE ELSE
ii_error_status = INVALID_FIELD
TriggerEvent( "ue_error" )
return ""
END CHOOSE
public int nf( ):
return ii_num_fields
public long nr( ):
return il_num_lines
Listing Seven
int i
uo_nv_awk luo_awk
luo_awk = CREATE uo_nv_awk
luo_awk.parse_line( 'Hello World' )
FOR I = 1 TO luo_awk.nf( )
messagebox( 'test', 'field '+string(i)+' is '+luo_awk.field( i ) )
NEXT
DESTROY iuo_nv_awk
??